package cn.zclong.data.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;

import cn.itcast.commons.CommonUtils;
import cn.itcast.jdbc.TxQueryRunner;
import cn.zclong.data.domain.Data;

public class DataDao {
	private QueryRunner qr = new TxQueryRunner();
	
	public void add(Data data) {
		try {
			String sql = "insert into data values(?,?,?)";
			Object[] params = {data.getId(), data.getFilename(), data.getPath()};
			qr.update(sql, params);			
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 查询所有
	 */
	public List<Data> findAll() {
		try {
			String sql = "select * from data";
			return qr.query(sql, new BeanListHandler<Data>(Data.class));
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	
	/*
	 * 查询详细详细
	 */
	public Data load(String id) {
		try {			
			String sql = "select * from data where id=?";
			return qr.query(sql, new BeanHandler<Data>(Data.class), id);
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 删除资源
	 */
	public void delete(String id) {
		try {
			String sql = "delete from data where id=?";
			qr.update(sql, id);
		} catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
